package com.aischool.api;

import com.aischool.common.kit.Md5Kit;
import com.aischool.common.model.SysUser;
import com.aischool.params.SysUserParams;
import com.aischool.routes.RouteMapping;
import com.aischool.service.SysAppService;
import com.aischool.service.SysRoleService;
import com.aischool.service.SysUserAppService;
import com.aischool.service.SysUserRoleService;
import com.aischool.service.SysUserService;
import com.aischool.utils.AppUtil;
import com.aischool.utils.DateUtil;
import com.aischool.utils.JsonCodeEnum;
import com.aischool.utils.JsonResult;
import com.aischool.utils.StateEnum;
import com.alibaba.fastjson.JSONObject;
import com.jfinal.aop.Inject;
import com.jfinal.core.Controller;
import com.jfinal.kit.StrKit;

@RouteMapping(url = "/api/user")
public class UserController extends Controller {
	
	@Inject
	SysUserService sysUserService;
	@Inject
	SysAppService sysAppService;
	@Inject
	SysUserAppService sysUserAppService;
	@Inject
	SysRoleService sysRoleService;
	@Inject
	SysUserRoleService sysUserRoleService;

	/**
	 * 获取用户列表
	 */
	public void getPage() {
		// 结果类
		JsonResult jr = new JsonResult();
		// 参数类
		SysUserParams params = new SysUserParams();
		// 获取参数
		params.setPageNo(getParaToInt("pageNo", 1));
		params.setPageSize(getParaToInt("pageSize", AppUtil.Default_PageSize));
		params.setAppId(getInt("so_app_id", 0));
//		if (getParaToInt("state")!=null) {
//			params.setState(getParaToInt("state"));
//		}
		params.setState(getInt("state", StateEnum.ALL.getState()));
		if (StrKit.notBlank(get("keywords",""))) {
			params.setKeywords(get("keywords",""));
		}
		// 获取分页
		Object pageData = sysUserService.getPage(params);
		
		jr.setDicKV("pageData", pageData);
		
		jr.setEnum(JsonCodeEnum.Success);
		renderJson(jr.Dic);
	}
	
	/**
	 * 获取单个用户
	 */
	public void getUser() {
		// 结果类
		JsonResult jr = new JsonResult();
		// 获取单个用户信息
		int user_id = getParaToInt("id",0);
		if (user_id > 0) {
			Object obj = sysUserService.getUser(user_id);
			if (obj != null) { jr.setDicKV("userinfo", obj); }
			
			obj = sysUserAppService.getUserApp(user_id);
			if (obj != null) { jr.setDicKV("userapp", obj); }
			
			obj = sysUserRoleService.getUserRole(user_id);
			if (obj != null) { jr.setDicKV("userrole", obj); }
		}
		
		JSONObject job = new JSONObject();
		Object obj = sysAppService.getSysAppAll();
		if (obj != null) { job.put("listapp", obj); }
		obj = sysRoleService.getSysRoleAll();
		if (obj != null) { job.put("listrole", obj); }
		jr.setDicKV("initdata", job);

		jr.setEnum(JsonCodeEnum.Success);
		renderJson(jr.Dic);
	}
	
	public void saveUser() {
		// 结果类
		JsonResult jr = new JsonResult();
		// 表单转换类
		int user_id = getParaToInt("user_id", 0);
		SysUser info = getModel(SysUser.class, "", true);
		
		info.setUserMobile(getPara("user_mobile",""));
		info.setUserEmail(getPara("user_email",""));
		
		// 复选框转换
		info.setIsMobile(getPara("is_mobile")!=null?1:0);
		info.setIsEmail(getPara("is_email")!=null?1:0);
		info.setIsLock(getPara("is_lock")!=null);
		
		// 密码
		String pwd1 = getPara("user_pwd","");
		if (pwd1.length()>0) {
			info.setLoginPwd(Md5Kit.MD5For8(pwd1));
		}
		
		info.setRegTime(null);
		info.setUpdateTime(DateUtil.getTimestamp());
		
		info.removeNullValueAttrs();
		
		boolean bf = false;
		
		if (StrKit.isBlank(info.getLoginName())) {
			jr.setMsg("请输入登录名！");
		}else if (user_id <= 0 && StrKit.isBlank(info.getLoginPwd())) {
			jr.setMsg("请输入登录密码！");
		}else if (StrKit.isBlank(info.getUserMobile())) {
			jr.setMsg("请输入手机！");
		}else if(sysUserService.getUserByLoginName(info.getLoginName())!=null){
			jr.setMsg("用户名已存在！请重新输入");
		}else if(sysUserService.getUserByMobile(info.getUserMobile())!=null){
			jr.setMsg("手机号已注册，请更换手机号！");
		}else {
			
			if (user_id >0) {
				// 更新
				bf = info.update();
			}else {
				// 插入
				info.setRegTime(DateUtil.getTimestamp());
				bf = info.save();
				if (bf) {
					user_id = info.getUserId();
				}
			}
			
			if (bf && user_id > 0) {
				// 更新所属应用
				sysUserAppService.updateUserApp(user_id, getRequest());
				// 更新所属角色
				sysUserRoleService.updateUserRole(user_id, getRequest());
				
				sysUserService.removeCache(user_id);
				
			}

			
			if (bf) {
				jr.setEnum(JsonCodeEnum.Success);
			}else {
				jr.setEnum(JsonCodeEnum.Faild);
			}
		}
		renderJson(jr.Dic);
	}
	
	public void del() {
		// 结果类
		JsonResult jr = new JsonResult();
		boolean bf = false;
		// 表单转换类
		int user_id = getParaToInt("id", 0);
		if (user_id > 0) {
			bf = sysUserService.delUser(user_id);
		}else {
			String ids = get("ids","");
			bf = sysUserService.delMore(ids);
		}
		
		if (bf) {
			jr.setEnum(JsonCodeEnum.Success);
		}else {
			jr.setEnum(JsonCodeEnum.Faild);
		}
		
		renderJson(jr.Dic);
	}
	
	public void checkAuth() {
		// 结果类
		JsonResult jr = new JsonResult();
		jr.setEnum(JsonCodeEnum.Success);
		renderJson(jr.Dic);
	}
}
